<template>
	<div class="system-role-dialog-container">
		<el-dialog :title="state.dialog.title" v-model="state.dialog.isShowDialog" width="769px">
			<el-form ref="roleDialogFormRef" :model="state.ruleForm" :rules="state.rules" size="default" label-width="90px">
				<el-row :gutter="35">
					<el-col :xs="24" :sm="12" :md="12" :lg="12" :xl="12" class="mb20">
						<el-form-item label="名称" prop="name">
							<el-input v-model="state.ruleForm.name" placeholder="请输入名称" clearable></el-input>
						</el-form-item>
					</el-col>
					<el-col :xs="24" :sm="12" :md="12" :lg="12" :xl="12" class="mb20">
						<el-form-item label="权限字符" prop="code">
							<el-input v-model="state.ruleForm.code" placeholder="请输入权限字符" clearable></el-input>
						</el-form-item>
					</el-col>
					<el-col :xs="24" :sm="24" :md="24" :lg="24" :xl="24" class="mb20">
						<el-form-item label="数据范围" prop="dataScope">
							<el-select v-model="state.ruleForm.dataScope" placeholder="取角色管理" clearable class="w100">
								<el-option
									v-for="item in scopeData"
									:key="'role-'+item.value"
									:label="item.label"
									:value="item.value"></el-option>
							</el-select>
							<el-tree
								:data="deptData"
								ref="deptRef"
								v-if="state.ruleForm.dataScope === '2'"
								show-checkbox
								default-expand-all
								node-key="id"
								class="w100 menu-data-tree tree-border"
								style="height:100px;overflow-y: auto;"
								:default-checked-keys="state.dataScopeDeptIds"
								:props="{children: 'children',label: 'name'}">
							</el-tree>
						</el-form-item>
					</el-col>
					<el-col :xs="24" :sm="24" :md="24" :lg="24" :xl="24" class="mb20">
						<el-form-item label="菜单权限">
							<el-tree style="height:200px;overflow-y: auto;"
								:data="menuData"
								ref="menuRef"
								class="w100 menu-data-tree tree-border"
								:default-checked-keys="state.menuIds"
								node-key="id"
								show-checkbox :props="{children: 'children',label: 'title'}"></el-tree>
						</el-form-item>
					</el-col>
				</el-row>
			</el-form>
			<template #footer>
				<span class="dialog-footer">
					<el-button @click="onCancel" size="default">取 消</el-button>
					<el-button type="primary" @click="onSubmit" size="default">{{ state.dialog.submitTxt }}</el-button>
				</span>
			</template>
		</el-dialog>
	</div>
</template>

<script setup lang="ts" name="systemRoleDialog">
import { reactive, ref, nextTick } from 'vue';
import constConfig from '/@/utils/constConfig';
import { roleApi } from '/@/api/system/role';
import { menuApi } from '/@/api/system/menu';

// 定义子组件向父组件传值/事件
const emit = defineEmits(['refresh']);

// 定义变量内容
const roleDialogFormRef = ref();
const menuRef = ref();
const deptRef = ref();
const props = defineProps({
    deptData:{
      type:Array,
      default:()=>[]
    },
    menuData:{
      type:Array,
      default:()=>[]
    },
    scopeData:{
      type:Array,
      default:()=>[]
    }
})

const state = reactive({
	ruleForm: {} as RowRoleType,
	dataScopeDeptIds:[],
    menuIds:[],
	dialog: {
		isShowDialog: false,
		type: '',
		title: '',
		submitTxt: '',
	},
	rules: {
		name: { required: true, message: "名称不能为空", trigger: "blur" },
		code: { required: true, message: "权限字符不能为空", trigger: "blur" },
		dataScope: { required: true, message: "数据范围不能为空", trigger: "blur" },
	},
});

// 打开弹窗
const openDialog = (type: string, row: RowRoleType) => {
	state.dialog.type = type;
	state.dialog.isShowDialog = true;
	state.ruleForm = row;
	if (type === constConfig.dialogType.add) {
		state.dialog.title = '新增角色';
		state.dialog.submitTxt = '新 增';
	} else {
		roleApi().getScopeDeptIdsByRoleId({roleId: row.id}).then((res:any)=>{
          state.dataScopeDeptIds = res.data
        })
        menuApi().getRoleMenu({roleId: row.id}).then((res:any)=>{
          state.menuIds = res.data
        })
		state.dialog.title = '修改角色';
		state.dialog.submitTxt = '修 改';
	}
	nextTick(() => {
		menuRef.value?.setCheckedKeys([]);
		deptRef.value?.setCheckedKeys([]);
		roleDialogFormRef.value.clearValidate();
	});
};
// 关闭弹窗
const closeDialog = () => {
	state.dialog.isShowDialog = false;
};
// 取消
const onCancel = () => {
	closeDialog();
};
const getMenuAllCheckedKeys = () => {
	return menuRef.value.getCheckedKeys();
}
const getDeptAllCheckedKeys = () => {
	return deptRef.value?.getCheckedKeys();
}
// 提交
const onSubmit = () => {
	roleDialogFormRef.value.validate((valid: boolean) => {
		if (valid) {
			state.ruleForm.resIds = getMenuAllCheckedKeys();
			state.ruleForm.deptIds = getDeptAllCheckedKeys();
			if (state.dialog.type === constConfig.dialogType.add) {
				roleApi().save(state.ruleForm).then(() =>{
					closeDialog();
					emit('refresh');
				})
			} else {
				roleApi().update(state.ruleForm).then(()=>{
					closeDialog();
					emit('refresh');
				})
			}
		}
	})
};
// 暴露变量
defineExpose({
	openDialog,
});
</script>
